package woaini.fenger.core.command.system;

import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import woaini.fenger.core.bot.event.Messages;
import woaini.fenger.core.bot.session.Session;
import woaini.fenger.core.command.ICmd;
import woaini.fenger.core.command.anno.CmdParams;
import woaini.fenger.core.command.anno.SubCmd;
import woaini.fenger.core.task.BotTaskManager;
import woaini.fenger.core.task.vo.QuartzJobsVO;

import java.util.List;

/**
 * 任务CMD任务
 *
 * @see woaini.fenger.core.command.system.TaskCmd
 * @author yefeng {@code @Date} 2023-05-16 16:50:39
 */
@Component
@AllArgsConstructor
public class TaskCmd implements ICmd {

  private BotTaskManager botTaskManager;

  @Override
  public String masterCmdName() {
    return "定时任务";
  }

  @Override
  public String description() {
    return "控制定时任务";
  }

  @SubCmd(value = "列表", description = "获取当前所有定时任务")
  public void reply(Session session) {
    List<QuartzJobsVO> allJob = botTaskManager.getAllJob();
    StringBuilder stringBuilder = new StringBuilder();
    for (QuartzJobsVO jobsVO : allJob) {
      stringBuilder
          .append("任务名称:")
          .append(jobsVO.getJobDetailName())
          .append("\r\n").append("cron:")
          .append(jobsVO.getJobCronExpression())
          .append("\n\n").append("状态:")
          .append(jobsVO.getStatus())
          .append("\r\n");
    }
    session.replyMessage(new Messages().text(StrUtil.isEmpty(stringBuilder.toString())?"暂无定时任务" : stringBuilder.toString()));
  }

  @SubCmd(value = "删除", description = "删除某个定时任务")
  public void delete(Session session,
                     @CmdParams(value = "-n", description = "名称",required = true) String name,
                     @CmdParams(value = "-g", description = "分组",required = true) String group) {
    boolean task = botTaskManager.deleteTask(name, group);
    session.replyMessage(new Messages().text(task ? "删除成功" : "删除失败"));
  }
}
